package com.petadoption.serverclient.controller;

import com.petadoption.serverclient.entity.Deliver;
import com.petadoption.serverclient.entity.Examinedeliver;
import com.petadoption.serverclient.entity.Pet;
import com.petadoption.serverclient.service.IDeliverService;
import com.petadoption.serverclient.service.IExaminedeliverService;
import com.petadoption.serverclient.service.IPetService;
import com.petadoption.serverclient.utils.Result;
import com.petadoption.serverclient.vo.DeliverPetVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 汪仕杰
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/examinedeliver")
public class ExaminedeliverController {
    @Resource
    private Result result;
    @Resource
    private IDeliverService deliverService;

    @Resource
    private IPetService petService;

    @Resource
    private IExaminedeliverService examinedeliverService;

    @PostMapping("/sendinfo")
    public Result addDeliver(@RequestBody DeliverPetVO deliverPetVO, HttpServletRequest request) {
        Pet pet = new Pet();
        pet.setPetName(deliverPetVO.getPetName());
        pet.setPetAge(deliverPetVO.getPetAge());
        pet.setPetSex(deliverPetVO.getPetSex());
        pet.setPetType(deliverPetVO.getPetType());
        pet.setPetBreed(deliverPetVO.getPetBreed());
        pet.setPetVaccine(deliverPetVO.getPetVaccine());
        pet.setPetInisect(deliverPetVO.getPetInisect());
        pet.setPetSterilization(deliverPetVO.getPetSterilization());
        pet.setPetDescr(deliverPetVO.getPetDescr());
        pet.setPetImg(deliverPetVO.getPetImg());
        //宠物添加
        int i = petService.addPet(pet);
        if (i < 0) return result.fail("送养宠物失败");
        Deliver deliver = new Deliver();
        deliver.setDeliverPet(pet.getPetId());
        deliver.setDeliverAvatar("http://dingyue.ws.126.net/2021/0909/3df9e6fcj00qz5vmz0015c000hs00hsc.jpg");
        deliver.setDeliverName(deliverPetVO.getDeliverName());
        deliver.setDeliverPhone(deliverPetVO.getDeliverPhone());
        deliver.setDeliverCity(deliverPetVO.getDeliverCity());
        deliver.setDeliverAddress(deliverPetVO.getDeliverAddress());
        int i1 = deliverService.addDeliver(deliver);
        if (i1 < 0) return result.fail("送养宠物失败");
        //审核添加
        Examinedeliver examinedeliver = new Examinedeliver();
        examinedeliver.setExaminedeliverName(deliver.getDeliverId());
        examinedeliver.setExaminedeliverTime(LocalDateTime.now());
        examinedeliver.setExaminedeliverPet(pet.getPetId());
        int i2 = examinedeliverService.addExamineDeliver(examinedeliver);
        if (i2 < 0) return result.fail("送养宠物失败");
        return result.success("宠物审核中...");
    }
}
